﻿using JESAI.Application.Abstractions.Admin.Dtos.Dict;
using JESAI.Application.Abstractions.Dtos;
using JESAI.Application.Abstractions.DynamicApi;
using JESAI.Application.Abstractions.DynamicApi.Dtos;
using JESAI.AutoMapper.ObjectMapping;
using JESAI.Core;
using JESAI.Core.Attributes;
using JESAI.Core.Extensions;
using JESAI.MonacoRoslynComplete.Models;
using JESAI.MonacoRoslynConplete.Storages;
using JESAI.Platform.Domain.DynamicApi.Entities;
using JESAI.Platform.Domain.DynamicApi.Repositories;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace JESAI.Application.Service.DynamicApi
{
    [AppService]
    public class DynamicApiService : IDynamicApiService
    {
        private readonly IDynamicApiRepository _dynamicApiRepository;
        private readonly IUser _user;
        private readonly IMonacoRoslynCompleteStorage _monacoRoslynCompleteStorage;
        public DynamicApiService(IDynamicApiRepository dynamicApiRepository, IUser user, IMonacoRoslynCompleteStorage monacoRoslynCompleteStorage)
        {
            _dynamicApiRepository = dynamicApiRepository;
            _user = user;
            _monacoRoslynCompleteStorage = monacoRoslynCompleteStorage;
        }
        public async Task DeleteDynamicApi(string id)
        {
            await _dynamicApiRepository.DeleteByIdAsync(id);
        }

        public async Task DeleteDynamicApis(string[] ids)
        {
            await _dynamicApiRepository.DeleteByIdsAsync(ids);
        }

        public async Task<DynamicApiDto> GetDynamicApiByApiSign(string apiSign, JESAI.Biz.Data.Enums.HttpType httpType)
        {
            var model = _dynamicApiRepository._DbQueryable.Where(p => p.ApiSign == apiSign && p.HttpType == (HttpType)httpType).ToList().FirstOrDefault();
            return model.MapTo<DynamicApiDto>();
        }

        public async Task<BasePageOutput<List<DynamicApiDto>>> QueryPageDynamicApis(QueryDynamicApiPageParam param)
        {
            var totalNumber = 0;
            var modelList = _dynamicApiRepository._DbQueryable
                               .WhereIF(param.IsValid.HasValue, it => it.IsValid == param.IsValid)
                               .WhereIF(!param.ApiName.IsEmpty(), it => it.ApiName == param.ApiName)
                               .WhereIF(param.CreateTime.HasValue, it => it.CreateTime == param.CreateTime)
                               .WhereIF(param.UpdateTime.HasValue, it => it.UpdateTime == param.UpdateTime)
                               .WhereIF(param.HttpType.HasValue, it => it.HttpType == (HttpType)param.HttpType.Value)
                                .WhereIF(!param.Description.IsEmpty(), it => it.Description == param.Description)
                                 .WhereIF(!param.ApiSign.IsEmpty(), it => it.ApiSign == param.ApiSign)
                                     .WhereIF(!param.EntryFunction.IsEmpty(), it => it.ApiSign == param.EntryFunction)
                               .ToPageList(param.PageIndex, param.PageSize, ref totalNumber);

            return new BasePageOutput<List<DynamicApiDto>> { Data = modelList.MapTo<List<DynamicApiDto>>(), CurrentPage = param.PageIndex, Total = totalNumber };
        }

        public async Task<List<ICodeFileModel>> QueryScriptCodeFileList()
        {
            return await _monacoRoslynCompleteStorage.GetScriptCodeFileListDatas();
        }

        public async Task SetDynamicApi(DynamicApiInput input)
        {
            var model = input.MapTo<DynamicApiEntity>();
            if (model.Id.IsNullOrEmpty())
            {
                model.Id = Guid.NewGuid().ToString();
                model.CreateTime = DateTime.Now;
                model.Cid = _user.Id.ToLong();
                await _dynamicApiRepository.InsertAsync(model);
            }
            else
            {
                model.UpdateTime = DateTime.Now;
                model.Uid = _user.Id.ToLong();
                await _dynamicApiRepository.UpdateAsync(model);
            }
        }
    }
}
